const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');


let listWindow;
function createListWindow(mainWindow) {
    
    listWindow = new BrowserWindow({
        x: 100,
        y: 100,
        show: false,
        width: 800,
        height: 600,
        alwaysOnTop: true,
        autoHideMenuBar: true,
        title: '主界面',
        webPreferences: {
            preload: path.join(__dirname, '/script/preload.js')
        }
    });

    listWindow.once('ready-to-show', () => {
        mainWindow.hide();//隐藏主界面
        listWindow.show();
    });

    listWindow.on('closed', () => {
        mainWindow.show();
        listWindow = null;
    });
    listWindow.loadFile(path.join(__dirname, '/list.html'));

    // listWindow.webContents.openDevTools();
}

//*************进程间通信**********/
//打开地图页面
ipcMain.on('open-map', () => {
    require('../mapPage/mapPage').createMapWindow();
});
//获取团期数据的ID
ipcMain.handle('get-tour-data-id', (event) => {
    return require('../temp/various').currentTourDateID;
})
//保存名单数据
ipcMain.on('save-list-data', (event, data, mergeCells) => {
    let various = require('../temp/various');
    various.tourDates[various.currentTourDateID - 1].list = data;
    various.tourDates[various.currentTourDateID - 1].mergeCells = mergeCells;
    // console.log(data);
    // console.log("++++++++++++++++++++");
    // console.log(various.tourDates);
})
//获取表格数据
ipcMain.handle('get-list-data', () => {
    let various = require('../temp/various');
    return various.tourDates[various.currentTourDateID - 1].list;
})
//获取表格合并数据
ipcMain.handle('get-list-data-merge', () => {
    let various = require('../temp/various');
    return various.tourDates[various.currentTourDateID - 1].mergeCells;
})
//保存定位的单元格属性
ipcMain.on('save-poi-info', (event, row, col, rowspan, placeTitle) => {
    let various = require('../temp/various');
    let mapPoi = various.tourDates[various.currentTourDateID - 1].mapPoi;
    if (mapPoi.length == 0) {
        mapPoi.push({
            row: row,
            col: col,
            rowspan: rowspan,
            placeTitle: placeTitle,
            poi: null
        });
        various.currentMapPoi_Ele = mapPoi[0];
    } else {
        for (let i = 0; i < mapPoi.length; i++) {
            if (mapPoi[i].row == row && mapPoi[i].col == col&&mapPoi[i].rowspan==rowspan) {
                mapPoi[i].placeTitle = placeTitle;
                various.currentMapPoi_Ele = mapPoi[i];
                break;
            }
            if (i == mapPoi.length - 1) {
                mapPoi.push({
                    row: row,
                    col: col,
                    rowspan: rowspan,
                    placeTitle: placeTitle,
                    poi: null
                });
                various.currentMapPoi_Ele = mapPoi[mapPoi.length - 1];
            }
        }
    }
    console.log(mapPoi);
})

module.exports = {
    createListWindow,
};